﻿using System;
using System.Data;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using log4net;
namespace AnyOffice.general.gzzd
{
    public partial class access : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected void Page_Load(object sender, EventArgs e)
        {
            int num = (Request.QueryString["disk_id"] != null) ? int.Parse(Request.QueryString["disk_id"].ToString()) : 0;
            DataTable table = new GzzdDao().SelectByDiskId(base.Conn, num);
            if (table.Rows.Count == 0)
            {
                this.litMessage.Text = base.ShowError("无此知识目录", "index.aspx");
            }
            else
            {
                string str = (table.Rows[0]["Name"] != DBNull.Value) ? table.Rows[0]["Name"].ToString() : "";
                string path = Server.MapPath("/attachment/gzzd/diskpath" + table.Rows[0]["DiskId"].ToString());
                string str3 = "," + ((table.Rows[0]["PostDept"] != DBNull.Value) ? table.Rows[0]["PostDept"].ToString() : "");
                string str4 = "," + ((table.Rows[0]["AccessUser"] != DBNull.Value) ? table.Rows[0]["AccessUser"].ToString() : "");
                string str5 = "," + ((table.Rows[0]["NewUser"] != DBNull.Value) ? table.Rows[0]["NewUser"].ToString() : "");
                string str6 = "," + ((table.Rows[0]["ManageUser"] != DBNull.Value) ? table.Rows[0]["ManageUser"].ToString() : "");
                string str7 = (table.Rows[0]["DownloadUser"] != DBNull.Value) ? ("," + table.Rows[0]["DownloadUser"].ToString()) : "";
                string str8 = (table.Rows[0]["DefaultPriv"] != DBNull.Value) ? table.Rows[0]["DefaultPriv"].ToString() : "";
                string str9 = (table.Rows[0]["ReadUser"] != DBNull.Value) ? ("," + table.Rows[0]["ReadUser"].ToString()) : "";
                string str10 = (table.Rows[0]["ReadPriv"] != DBNull.Value) ? ("," + table.Rows[0]["ReadPriv"].ToString()) : "";
                string str11 = (table.Rows[0]["EditUser"] != DBNull.Value) ? ("," + table.Rows[0]["EditUser"].ToString()) : "";
                bool flag = false;
                UserCache cache = new UserCache();
                GzzdInfo info = new GzzdInfo();
                info.Name = str;
                info.DiskPath = path;
                switch (Convert.ToInt32(table.Rows[0]["Type"].ToString()))
                {
                    case 1:
                        flag = true;
                        break;

                    case 2:
                        if (str3.IndexOf("," + cache.DeptId.ToString() + ",") >= 0)
                        {
                            flag = true;
                        }
                        break;

                    case 3:
                        if (str4.IndexOf("," + base.User.Identity.Name + ",") >= 0)
                        {
                            flag = true;
                        }
                        break;
                }
                if (str8.IndexOf("FileRead") > -1)
                {
                    info.CanRead = true;
                }
                if (str8.IndexOf("FilePrint") > -1)
                {
                    info.CanPrint = true;
                }
                if (str8.IndexOf("FileCopy") > -1)
                {
                    info.CanCopy = true;
                }
                if (str8.IndexOf("FileDownload") > -1)
                {
                    info.CanDownload = true;
                }
                if (str5.IndexOf("," + base.User.Identity.Name + ",") >= 0)
                {
                    flag = true;
                    info.CanNew = true;
                }
                if (str9.IndexOf("," + base.User.Identity.Name + ",") > -1)
                {
                    flag = true;
                    info.CanRead = true;
                    if (str10.IndexOf("FilePrint") > -1)
                    {
                        info.CanPrint = true;
                    }
                    if (str10.IndexOf("FileCopy") > -1)
                    {
                        info.CanCopy = true;
                    }
                }
                if (str11.IndexOf("," + base.User.Identity.Name + ",") > -1)
                {
                    flag = true;
                    info.CanEdit = true;
                }
                if (str7.IndexOf("," + base.User.Identity.Name + ",") >= 0)
                {
                    flag = true;
                    info.CanDownload = true;
                }
                if (str6.IndexOf("," + base.User.Identity.Name + ",") >= 0)
                {
                    flag = true;
                    info.CanNew = true;
                    info.CanRead = true;
                    info.CanPrint = true;
                    info.CanCopy = true;
                    info.CanEdit = true;
                    info.CanDownload = true;
                    info.CanManage = true;
                }
                if (!flag)
                {
                    this.litMessage.Text = base.ShowError("无此知识目录", "index.aspx");
                }
                else
                {
                    try
                    {
                        if (Directory.Exists(path))
                        {
                            GzzdFactory.AddGzzdInfo(info);
                            base.CloseConn();
                            Response.Redirect("gzzd.aspx");
                        }
                        else
                        {
                            this.litMessage.Text = base.ShowError("目录路径不正确!", "index.aspx");
                        }
                    }
                    catch (Exception exception)
                    {
                        if (log.IsErrorEnabled)
                        {
                            log.Error("", exception);
                        }
                        this.litMessage.Text = base.ShowError("目录路径不正确!", "index.aspx");
                    }
                }
            }
        }
    }
}
